/**
 * @Method Description
 * @Author: hao zheng@
 * @Description:In User Settings Edit
 * @LastEditors: Please set LastEditors
 * @param {}
 * @return 
 * @createTime: 2021-7-9 11:15:13
 */


import {
	request
} from '@/common/js/request'
const bassUrl = require('@/common/js/config.js').bass;
import {
	msg
} from "@/common/js/util.js"
import QQMapWX from "@/common/js/qqmap-wx-jssdk.js"

export default {
	data() {
		return {
			isLoading: false, //刷新数据
			loaded: false //加载完毕
		}
	},
	methods: {
		refresh() {
			this.$nextTick(() => {
				this.$refs.pullScroll.refresh();
			});
		},
		/**
		 * @Description point something to window
		 */
		back() {
			setTimeout(() => {
				uni.navigateBack()
			}, 500)
		},
		/**
		 * @Description setNavigationBarTitle
		 */
		setNavTitle(title) {
			if (!title) return;
			uni.setNavigationBarTitle({
				title
			})
		},
		/**
		 * @Description Navigate to  some page of this project and judge user has login
		 * @options {}  judge user has login
		 * @url {}  Navigate to  some page path
		 */
		navTo(url, options = {}) {
			console.log(url)
			this.$util.throttle(() => {
				if (!url) {
					return;
				}
				if (options.login && !uni.getStorageSync('uid')) {
					url = '/pages/public/author';
				}
				uni.navigateTo({
					url: url
				})
			}, 100)
		},
		/**
		 * @Description setClipboardData
		 * @content {}  content
		 */
		setClipboardData(content) {
			if (!content) return;
			uni.setClipboardData({
				data: content,
				success: function() {}
			});
		},
		callphone(phone) {
			console.log(phone)
			this.$util.throttle(() => {
				if (!phone) {
					return;
				}
				uni.makePhoneCall({
					phoneNumber: phone
				})
			}, 100)
		},
		getUserLoction() {
			let point = {};
			return new Promise((resolve, reject) => {
				uni.getLocation({
					type: 'gcj02',
					success(res) {
						point = res;
						uni.setStorageSync('point', point);
						let qqmapsdk = new QQMapWX({
							key: 'GQZBZ-3CECD-BP443-PBQEY-4R3M6-QMFBO'
						});
						qqmapsdk.reverseGeocoder({
							location: {
								latitude: res.latitude,
								longitude: res.longitude
							},
							success: function(options) {
								resolve(options)
							}
						});
					},
					fail() {
						resolve()
					}
				})

			})
		},
		bannerItemTap(item) {
			console.log(item)
			//  "type":""//跳转类型0富文本1xx页面2345678910
			getApp().globalData.bannerItem = item;
			if (item.type == 0) {
				this.navTo("/pages/public/picview");
			} else if (item.type == 1) {
				// this.navTo(`/pages/details/goodDetails?id=${item.goods.id}`);
			}
		},
		/**
		 * @Description user options cart
		 * @item {}  user add cart item (goods)
		 * @type user add cart or delete cart  type 1  add  type -1   delete
		 */
		async addCart(id, type, souce) {
			const data = {
				uid: uni.getStorageSync('uid'),
				goodsId:id,
				type
			}
			const res = await this.$request('goods/add-wash-bag', data, {
				login: true
			});
			if (res) {
				if (res.result == 0) {
					if (souce == "list") {
						this.$util.msg('加入洗衣篮成功!');
					}else{
						type=='1'?this.inputValue++:this.inputValue--;
					}
				} else {
					this.$util.msg(res.resultNote)
				}
			}
		},
		/**
		 * @Description get data from  back-end and judge user has login
		 * @options {}  judge user has login
		 * @url string request back-end interface  need path
		 * @data {}  request back-end interface need parameter
		 */
		$request(url, data = {}, options = {}) {
			if (options.login && !uni.getStorageSync('uid')) {
				uni.navigateTo({
					url: '/pages/public/author'
				})
				return
			}
			if (options.showLoading) {
				this.isLoading = true;
			}
			return new Promise((resolve, reject) => {
				request(url, data, options).then(res => {
					if (res.statusCode == 200) {
						resolve(res.data)
					} else {
						this.$util.msg(`错误码:${res.statusCode}`);
						this.$refs.pullScroll && this.$refs.pullScroll.empty();
					}
					setTimeout(() => {
						this.isLoading = false;
						this.loaded = true;
					}, 500)
				}).catch((err) => {
					reject(err);
				})
			})
		},
		/**
		 * @Description upload file to back-end
		 * @url string   request back-end interface  need path
		 * @data {}  request back-end interface need parameter
		 */
		$uploadFile(url, data = {}) {
			return new Promise((e, n) => {
				const uploadTask = uni.uploadFile({
					url: encodeURI(bassUrl + '/api/' + url),
					filePath: data.type == 0 ? data.filePath.path : data.filePath,
					name: 'file',
					method: "POST",
					headers: {
						'Content-Type': 'application/json;',
						'Content-Type': 'multipart/form-data;'
					},
					success: function(res) {
						200 == res.statusCode ? e(res.data) : msg(`错误码:${res.statusCode}`);
					},
					fail: function(err) {
						"request:fail " === err.errMsg && msg("请求数据失败！"), n(err.data);
					}
				});
				uploadTask.onProgressUpdate((res) => {
					data.progress = res.progress;

				});
			})
		},
		/**
		 * @Description show image is in lazyload but this is only  use  scoll-view
		 * @data  current can show  image
		 */
		imageOnLoad(data, key) {
			setTimeout(() => {
				this.$set(data, 'loaded', true);
			}, 100)
		},
		/**
		 * @Description the templete has  show  do  something
		 * @key string  the templete name  or ref
		 */
		showPopup(key) {
			this.$util.throttle(() => {
				this.$refs[key].open();
			}, 200)
		},
		/**
		 * @Description the templete has  hide  do  something
		 * @key string  the templete name  or ref
		 */
		hidePopup(key) {
			this.$refs[key].hidden();
		},
		stopPrevent() {}
	}
}
